package com.ttc.ssm.controller.home;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.ttc.ssm.common.JobPage;
import com.ttc.ssm.common.ManagePage;
import com.ttc.ssm.common.ResumePage;
import com.ttc.ssm.common.SearchResumePage;
import com.ttc.ssm.mapper.home.HomeJobMapper;
import com.ttc.ssm.po.*;
import com.ttc.ssm.service.admin.CommonService;
import com.ttc.ssm.service.admin.CustomerService;
import com.ttc.ssm.service.home.HomeJobService;
import com.ttc.ssm.service.home.HomeResumeService;
import com.ttc.ssm.utils.DateUtils;
import com.ttc.ssm.utils.FileUtils;
import com.ttc.ssm.utils.TextUtils;
import org.aspectj.util.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.*;

/**
 * Created by 58296 on 2016/8/9.
 */
@Controller
@RequestMapping("home/resume")
public class HomeResumeController {

    @Autowired
    CommonService commonService;

    @Autowired
    HomeResumeService homeResumeService;

    @Autowired
    CustomerService customerService;

    @Autowired
    HomeJobService homeJobService;

    @RequestMapping("index")
    public String index(Model model) throws Exception
    {
        return "home/resume/index";
    }

    @RequestMapping("/getResumeList")
    @ResponseBody
    public Map getResumeList(HttpSession session, @RequestParam("p") Integer page) throws Exception
    {
        int customer_id = (Integer) session.getAttribute("customer_id");
        System.out.println(page);
        ResumePage resumePage = new ResumePage();
        resumePage.setPageSize(10);
        resumePage.setCurrentPage(page);
        if(customer_id>0){
            resumePage.setCustomer_id(customer_id);
        }
        resumePage.setTotalRows(homeResumeService.totalResumeRows(resumePage));
        List<Resume> resumes = homeResumeService.selectResumeForPage(resumePage);
        Map map = new HashMap();
        map.put("data",resumes);
        map.put("show",resumePage);
        System.out.println(resumes);
        return map;

    }

    @RequestMapping("create")
    public String create(Model model) throws Exception
    {
        List<Zone> zoneList = commonService.getZone();
        List<City> cityList = commonService.getCityByZoneId(zoneList.get(0).getZone_id());
        List<Industry> industryList = commonService.getIndustry();
        List<JobType> jobTypeList = commonService.getJobTypeByIndustryId(industryList.get(0).getIndustry_id());
        List<SalaryType> salaryTypeList = commonService.getSalaryType();
        List<Degree> degreeList = commonService.getDegree();
        List<People> peopleList = commonService.getPeople();
        model.addAttribute("zoneList", zoneList);
        model.addAttribute("cityList", cityList);
        model.addAttribute("industryList",industryList);
        model.addAttribute("jobTypeList", jobTypeList);
        model.addAttribute("salaryTypeList", salaryTypeList);
        model.addAttribute("degreeList", degreeList);
        model.addAttribute("peopleList", peopleList);
        return "home/resume/create";
    }

    @RequestMapping("/getCityList")
    @ResponseBody
    public List<City> getCityList(@RequestParam("zone_id") Integer zone_id) throws Exception
    {
        return commonService.getCityByZoneId(zone_id);
    }

    @RequestMapping("/getJobTypeList")
    @ResponseBody
    public List<JobType> getJobTypeList(@RequestParam("industry_id") Integer industry_id) throws Exception
    {
        return commonService.getJobTypeByIndustryId(industry_id);
    }

    @RequestMapping("/insertResume")
    @ResponseBody
    public Object insertResume(HttpSession session,HttpServletRequest request, Resume resume, @RequestParam("input13") MultipartFile temp_file) throws Exception
    {
        try {
            if (resume.getBirthday_string()!=null) {
                Date date = DateUtils.stringToDate(resume.getBirthday_string());
                resume.setBirthday(date);
            }
            String name= FileUtils.createFile(temp_file, request);
            if (!TextUtils.isEmpty(name)) {
                resume.setAttachment(name);
            }
            int customer_id = (Integer) session.getAttribute("customer_id");
            resume.setCustomer_id(customer_id);
            System.out.println(resume.getComment());
            int resume_id = homeResumeService.insertResumeService(resume);
            System.out.println("==================>"+resume_id);
            return resume_id;
        }catch (Exception e) {

            return 0;
        }

    }

    @RequestMapping("/insertWorkExp")
    @ResponseBody
    public int insertWorkExp(@RequestBody WorkExp workExp) throws Exception
    {
        System.out.println(workExp.getArr());
        if (workExp == null || workExp.getArr() == null || workExp.getResume_id() <= 0) {
            return -1;
        }
        try {
            System.out.println(workExp.getArr());
            WorkExp temp;
            Iterator iterator = workExp.getArr().iterator();
            while (iterator.hasNext()) {
                List list;
                if ((list = (List) iterator.next())!=null) {
                    temp = new WorkExp();
                    temp.setResume_id(workExp.getResume_id());
                    String text = (String) list.get(3);
                    String date_start = (String) list.get(1);
                    String date_end = (String) list.get(2);
                    if (!TextUtils.isEmpty(text) && !TextUtils.isEmpty(date_end) && !TextUtils.isEmpty(date_start)) {
                        temp.setText(text);
                        temp.setDate_start(DateUtils.stringToDate(date_start));
                        temp.setDate_end(DateUtils.stringToDate(date_end));
                        customerService.insertWork(temp);
                    }
                }
            }
            return 1;
        }catch (Exception e) {

            return 0;
        }
    }

    @RequestMapping("/insertComExp")
    @ResponseBody
    public int insertComExp(@RequestBody CompetitionExp competitionExp) throws Exception
    {
        if (competitionExp == null || competitionExp.getArr() == null || competitionExp.getResume_id()<=0) {
            return -1;
        }
        try {
            System.out.println(competitionExp.getArr());
            Iterator iterator = competitionExp.getArr().iterator();
            CompetitionExp temp;
            while (iterator.hasNext()) {
                List list;
                if ((list = (List) iterator.next())!=null) {
                    temp = new CompetitionExp();
                    temp.setResume_id(competitionExp.getResume_id());
                    String name = (String) list.get(1);
                    String obtain = (String) list.get(2);
                    String date = (String) list.get(3);
                    if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(obtain) && !TextUtils.isEmpty(date)) {
                        temp.setName(name);
                        temp.setObtain(obtain);
                        temp.setDate(DateUtils.stringToDate(date));
                        System.out.println(name);
                        customerService.insertCom(temp);
                    }
                }
            }
            return 1;
        }catch (Exception e) {

            return 0;
        }
    }

    @RequestMapping("/insertEdu")
    @ResponseBody
    public int insertEdu(@RequestBody Education education) throws Exception
    {
        if (education == null || education.getArr() == null || education.getResume_id() <= 0) {
            return -1;
        }
        try {
            Iterator iterator = education.getArr().iterator();
            Education temp;
            while (iterator.hasNext()) {
                List list;
                if ((list = (List) iterator.next())!=null) {
                    temp = new Education();
                    temp.setResume_id(education.getResume_id());
                    String school = (String) list.get(3);
                    String date_start = (String) list.get(1);
                    String date_end = (String) list.get(2);
                    if (!TextUtils.isEmpty(school) && !TextUtils.isEmpty(date_start) && !TextUtils.isEmpty(date_end)) {
                        temp.setSchool(school);
                        temp.setDate_start(DateUtils.stringToDate(date_start));
                        temp.setDate_end(DateUtils.stringToDate(date_end));
                        System.out.println(school);
                        customerService.insertEdu(temp);
                    }
                }
            }
            return 1;
        }catch (Exception e) {

            return 0;
        }
    }

    @RequestMapping("/insertSkill")
    @ResponseBody
    public int insertSkill(@RequestBody Skill skill) throws Exception
    {
        if (skill == null || skill.getArr() == null || skill.getResume_id()==null) {
            return -1;
        }
        try {
            Skill temp;
            Iterator iterator = skill.getArr().iterator();
            while (iterator.hasNext()) {
                String list;
                if ((list = (String) iterator.next())!=null) {
                    temp = new Skill();
                    temp.setResume_id(skill.getResume_id());
                    if (!TextUtils.isEmpty(list)) {
                        temp.setName(list);
                        System.out.println(temp.getName());
                        customerService.insertSkill(temp);
                    }
                }
            }
            return 1;
        }catch (Exception e) {

            return 0;
        }
    }

    @RequestMapping("detail")
    public String detail(Model model, @RequestParam("resume_id") Integer resume_id) throws Exception
    {
        Resume resume = homeResumeService.selectResumeById(resume_id);
        System.out.println(resume.getExpect_city().getZone_id());
        System.out.print(resume.getExpect_city().getZone().getName());
        model.addAttribute("resume", resume);
        List<WorkExp> workExpList = homeResumeService.selectWorkByResumeId(resume.getResume_id());
        model.addAttribute("workExpList",workExpList);
        List<Education> educationList = homeResumeService.selectEduByResumeId(resume.getResume_id());
        model.addAttribute("educationList",educationList);
        List<CompetitionExp> competitionExpList = homeResumeService.selectComByResumeId(resume.getResume_id());
        model.addAttribute("competitionExpList",competitionExpList);
        List<Skill> skillList = homeResumeService.selectSkillByResumeId(resume.getResume_id());
        model.addAttribute("skillList",skillList);
        System.out.print("**********************"+skillList.size()+"********************");
        return "home/resume/detail";
    }

    @RequestMapping("detailForCompany")
    public String detailForCompany(Model model , @RequestParam("resume_id") Integer resume_id) throws Exception{
        Resume resume = homeResumeService.selectResumeById(resume_id);
        System.out.println(resume.getExpect_city().getZone_id());
        System.out.print(resume.getExpect_city().getZone().getName());
        model.addAttribute("resume", resume);
        List<WorkExp> workExpList = homeResumeService.selectWorkByResumeId(resume.getResume_id());
        model.addAttribute("workExpList",workExpList);
        List<Education> educationList = homeResumeService.selectEduByResumeId(resume.getResume_id());
        model.addAttribute("educationList",educationList);
        List<CompetitionExp> competitionExpList = homeResumeService.selectComByResumeId(resume.getResume_id());
        model.addAttribute("competitionExpList",competitionExpList);
        List<Skill> skillList = homeResumeService.selectSkillByResumeId(resume.getResume_id());
        model.addAttribute("skillList",skillList);
        System.out.print("**********************"+skillList.size()+"********************");
        return "home/resume/detailForCompany";
    }

    @RequestMapping("deleteResumes")
    @ResponseBody
    public int deleteResumes(@RequestBody Map ids) throws Exception{
        try {
            customerService.deleteResumes(ids);
            return 1;
        }catch (Exception e){
            return 0;
        }
    }

    @RequestMapping("edit")
    public String edit(Model model, @RequestParam("resume_id") Integer resume_id) throws Exception
    {
        Resume resume = homeResumeService.selectResumeById(resume_id);
        List<Zone> zoneList = commonService.getZone();
        List<City> originCityList = commonService.getCityByZoneId(resume.getOrigin_city().getZone_id());
        List<City> liveCityList = commonService.getCityByZoneId(resume.getLive_city().getZone_id());
        List<City> expectCityList = commonService.getCityByZoneId(resume.getExpect_city().getZone_id());
        List<Industry> industryList = commonService.getIndustry();
        List<JobType> jobTypeList = commonService.getJobTypeByIndustryId(resume.getIndustry_id());
        List<SalaryType> salaryTypeList = commonService.getSalaryType();
        List<Degree> degreeList = commonService.getDegree();
        List<People> peopleList = commonService.getPeople();
        model.addAttribute("zoneList", zoneList);
        model.addAttribute("originCityList", originCityList);
        model.addAttribute("liveCityList", liveCityList);
        model.addAttribute("expectCityList", expectCityList);
        model.addAttribute("industryList",industryList);
        model.addAttribute("jobTypeList", jobTypeList);
        model.addAttribute("salaryTypeList", salaryTypeList);
        model.addAttribute("degreeList", degreeList);
        model.addAttribute("peopleList", peopleList);
        model.addAttribute("resume", resume);

        List<WorkExp> workExpList = homeResumeService.selectWorkByResumeId(resume.getResume_id());
        model.addAttribute("workExpList",workExpList);
        List<Education> educationList = homeResumeService.selectEduByResumeId(resume.getResume_id());
        model.addAttribute("educationList",educationList);
        List<CompetitionExp> competitionExpList = homeResumeService.selectComByResumeId(resume.getResume_id());
        model.addAttribute("competitionExpList",competitionExpList);
        List<Skill> skillList = homeResumeService.selectSkillByResumeId(resume.getResume_id());
        model.addAttribute("skillList",skillList);
        return "home/resume/edit";
    }

    @RequestMapping("/saveResume")
    @ResponseBody
    public Object saveResume(HttpServletRequest request, Resume resume, @RequestParam("input13") MultipartFile temp_file) throws Exception
    {
        try {
            if (resume.getResume_id() == null || resume.getRealname() == null) {
                return -1;
            }
            if (resume.getBirthday_string()!=null) {
                Date date = DateUtils.stringToDate(resume.getBirthday_string());
                resume.setBirthday(date);
            }
            String name = FileUtils.createFile(temp_file,request);
            if (!TextUtils.isEmpty(name)) {
                resume.setAttachment(name);
                Resume tmp_resume = customerService.selectResumeById(resume.getResume_id());
                if (tmp_resume.getAttachment() != null) {
                    FileUtils.deleteFile(tmp_resume.getAttachment(),request);
                }
            }
            System.out.println(resume.getComment());
            customerService.updateResumes(resume);
            return 1;
        }catch (Exception e) {

            return 0;
        }

    }

    @RequestMapping("/saveSkill")
    @ResponseBody
    public int saveSkill(@RequestBody Skill skill) throws Exception
    {
        if (skill == null || skill.getArr() == null || skill.getResume_id()==null) {
            return -1;
        }
        try {
            System.out.println(skill.getArr());
            Iterator iterator2 = skill.getArr().iterator();
            Skill temp;
            Map map = new HashMap();
            List<Integer> ids = getIds(iterator2);
            map.put("ids",ids);
            map.put("resume_id",skill.getResume_id());
            customerService.deleteSkillById(map);
            Iterator iterator = skill.getArr().iterator();
            while (iterator.hasNext()) {
                List list;
                if ((list = (List) iterator.next())!=null) {
                    temp = new Skill();
                    temp.setResume_id(skill.getResume_id());
                    String name = (String) list.get(1);
                    if (!TextUtils.isEmpty(name)) {
                        temp.setName(name);
                        System.out.println(temp.getName());
                        if (list.get(0) == null || list.get(0).equals("")) {
                            customerService.insertSkill(temp);
                        }else {
                            Integer skill_id = Integer.valueOf((String)list.get(0));
                            System.out.println(skill_id);
                            temp.setSkill_id(skill_id);
                            customerService.updateSkill(temp);
                        }
                    }

                }
            }
            return 1;
        }catch (Exception e) {

            return 0;
        }
    }

    @RequestMapping("/saveWorkExp")
    @ResponseBody
    public int saveWorkExp(@RequestBody WorkExp workExp) throws Exception
    {
        System.out.println(workExp.getArr());
        if (workExp == null || workExp.getArr() == null || workExp.getResume_id()==null) {
            return -1;
        }
        try {
            System.out.println(workExp.getArr());
            WorkExp temp;
            Iterator iterator2 = workExp.getArr().iterator();
            Map map = new HashMap();
            List<Integer> ids = getIds(iterator2);
            map.put("ids",ids);
            map.put("resume_id",workExp.getResume_id());
            customerService.deleteWorkById(map);
            Iterator iterator = workExp.getArr().iterator();
            while (iterator.hasNext()) {
                List list;
                if ((list = (List) iterator.next())!=null) {
                    temp = new WorkExp();
                    temp.setResume_id(workExp.getResume_id());
                    String text = (String) list.get(3);
                    String date_start = (String) list.get(1);
                    String date_end = (String) list.get(2);
                    if (!TextUtils.isEmpty(text) && !TextUtils.isEmpty(date_end) && !TextUtils.isEmpty(date_start)) {
                        temp.setText(text);
                        temp.setDate_start(DateUtils.stringToDate(date_start));
                        temp.setDate_end(DateUtils.stringToDate(date_end));
                        if (TextUtils.isEmpty((String) list.get(0))) {
                            customerService.insertWork(temp);
                        }else {
                            Integer work_exp_id = Integer.valueOf((String)list.get(0));
                            System.out.println(work_exp_id);
                            temp.setWork_exp_id(work_exp_id);
                            customerService.updateWork(temp);
                        }
                    }

                }
            }
            return 1;
        }catch (Exception e) {

            return 0;
        }
    }

    @RequestMapping("/saveComExp")
    @ResponseBody
    public int saveComExp(@RequestBody CompetitionExp competitionExp) throws Exception
    {
        if (competitionExp == null || competitionExp.getArr() == null || competitionExp.getResume_id()==null) {
            return -1;
        }
        try {
            System.out.println(competitionExp.getArr());
            Iterator iterator2 = competitionExp.getArr().iterator();

            Map map = new HashMap();
            List<Integer> ids = getIds(iterator2);
            map.put("ids",ids);
            map.put("resume_id", competitionExp.getResume_id());
            customerService.deleteComById(map);
            Iterator iterator = competitionExp.getArr().iterator();
            CompetitionExp temp;
            while (iterator.hasNext()) {
                List list;
                if ((list = (List) iterator.next())!=null) {
                    temp = new CompetitionExp();
                    temp.setResume_id(competitionExp.getResume_id());
                    String name = (String) list.get(1);
                    String obtain = (String) list.get(2);
                    String date = (String) list.get(3);
                    if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(obtain) && !TextUtils.isEmpty(date)) {
                        temp.setName(name);
                        temp.setObtain(obtain);
                        temp.setDate(DateUtils.stringToDate(date));
                        System.out.println(name);
                        if (TextUtils.isEmpty((String) list.get(0))) {
                            customerService.insertCom(temp);
                        }else {
                            Integer competition_exp_id = Integer.valueOf((String)list.get(0));
                            System.out.println(competition_exp_id);
                            temp.setCompetition_exp_id(competition_exp_id);
                            customerService.updateCom(temp);
                        }
                    }
                }
            }
            return 1;
        }catch (Exception e) {

            return 0;
        }
    }

    @RequestMapping("/saveEdu")
    @ResponseBody
    public int saveEdu(@RequestBody Education education) throws Exception
    {
        if (education == null || education.getArr() == null || education.getResume_id()==null) {
            return -1;
        }
        try {
            System.out.println(education.getArr());
            Iterator iterator2 = education.getArr().iterator();
            Map map = new HashMap();
            List<Integer> ids = getIds(iterator2);
            map.put("ids",ids);
            map.put("resume_id", education.getResume_id());
            customerService.deleteEduById(map);
            Iterator iterator = education.getArr().iterator();
            Education temp;
            while (iterator.hasNext()) {
                List list;
                if ((list = (List) iterator.next())!=null) {
                    temp = new Education();
                    temp.setResume_id(education.getResume_id());
                    String school = (String) list.get(3);
                    String date_start = (String) list.get(1);
                    String date_end = (String) list.get(2);
                    if (!TextUtils.isEmpty(school) && !TextUtils.isEmpty(date_start) && !TextUtils.isEmpty(date_end)) {
                        temp.setSchool(school);
                        temp.setDate_start(DateUtils.stringToDate(date_start));
                        temp.setDate_end(DateUtils.stringToDate(date_end));
                        System.out.println(school);
                        if (TextUtils.isEmpty((String) list.get(0))) {
                            customerService.insertEdu(temp);
                        }else {
                            Integer education_id = Integer.valueOf((String)list.get(0));
                            System.out.println(education_id);
                            temp.setEducation_id(education_id);
                            customerService.updateEdu(temp);
                        }
                    }
                }
            }
            return 1;
        }catch (Exception e) {

            return 0;
        }
    }

    private  List<Integer> getIds(Iterator iterator)
    {
        List<Integer> ids = new ArrayList();
        while (iterator.hasNext()) {
            List list;
            if ((list = (List) iterator.next())!=null) {
                if (!TextUtils.isEmpty((String) list.get(0))) {
                    Integer id = Integer.valueOf((String)list.get(0));
                    ids.add(id);
                }
            }
        }
        return ids;
    }

    @RequestMapping("/search")
    public String search(Model model , HttpSession session ) throws Exception{
        List<Degree> degreeList = homeResumeService.getDegree();
        model.addAttribute("degreeList",degreeList);
        List<SalaryType> salaryTypeList = homeResumeService.getSalaryType();
        model.addAttribute("salaryTypeList",salaryTypeList);
        List<Industry> industryList = homeResumeService.getIndustry();
        model.addAttribute("industryList",industryList);
        List<JobProperty> jobPropertyList = homeResumeService.getJobProperty();
        model.addAttribute("jobPropertyList",jobPropertyList);
        int com = (Integer) session.getAttribute("company_id");
        List<JobTag> jobTagList = homeResumeService.getJobTab(com);
        model.addAttribute("jobTagList",jobTagList);
        return "home/resume/search";
    }

    @RequestMapping("/searchResume")
    public String searchResume(Model model , SearchResumePage searchResumePage) throws Exception {
        System.out.println("*************************"+searchResumePage.getDegree_id()+" "+searchResumePage.getJobName()+" "+searchResumePage.getSex()+" "+searchResumePage.getIndustry_id()+" "+searchResumePage.getSalary_type_id()+" "+searchResumePage.getWork_year()+"*******************");
        if (searchResumePage.getIndustry_id() == null && searchResumePage.getResumeId() == null) {
            model.addAttribute("error", 2);
            return "forward:/home/resume/search.action";
        }
        if(searchResumePage.getResumeId()!=null){
            Resume resume = homeResumeService.selectResumeById(searchResumePage.getResumeId());
            if (resume == null) {
                model.addAttribute("error", 1);
                return "forward:/home/resume/search.action";
            }
            model.addAttribute("searchResumePage", searchResumePage);
            model.addAttribute("resume",resume);
        }else {
            if (searchResumePage.getSalary_type_id() == 0)
                searchResumePage.setSalary_type_id(6);
            if (searchResumePage.getDegree_id() == 0)
                searchResumePage.setDegree_id(8);
            int rows = homeResumeService.countRowsForSearchResume(searchResumePage);
            System.out.println(rows);
            if (rows == 0) {
                model.addAttribute("error", 1);
                return "forward:/home/resume/search.action";
            }
            model.addAttribute("searchResumePage", searchResumePage);
        }
        return "home/resume/searchResume";
    }

    @RequestMapping("/getSearchList")
    @ResponseBody
    public Map getSearchList(@RequestParam("p") Integer p, SearchResumePage searchResumePage) throws Exception{
        searchResumePage.setPageSize(10);
        searchResumePage.setCurrentPage(p);
        searchResumePage.setJobName(searchResumePage.getJobName().trim());
        searchResumePage.setSex(searchResumePage.getSex().trim());
        if(searchResumePage.getResumeId()!=null){

            Resume resume = homeResumeService.selectResumeById(searchResumePage.getResumeId());
            Map map = new HashMap();
            map.put("data",resume);
            map.put("show",searchResumePage);
            return map;
        }else{
            int rows = homeResumeService.countRowsForSearchResume(searchResumePage);
            System.out.println(rows);
            searchResumePage.setTotalRows(rows);
            List<Resume> resumeList = homeResumeService.selectResume(searchResumePage);
            Map map = new HashMap();
            map.put("data",resumeList);
            map.put("show",searchResumePage);
            return map;
        }
    }

    @RequestMapping("/getJobList")
    @ResponseBody
    public Map getJobList(HttpSession session, @RequestParam("p") Integer p) throws Exception{
       int company_id = (Integer) session.getAttribute("company_id");
        JobPage jobPage = new JobPage();
        jobPage.setPageSize(10);
        jobPage.setCurrentPage(p);
        if(company_id>0){
            jobPage.setCompany_id(company_id);
        }
        jobPage.setTotalRows(homeJobService.countRows(jobPage));
        List<Job> jobs = homeJobService.selectJobForPage(jobPage);
        Map map = new HashMap();
        map.put("data",jobs);
        map.put("show",jobPage);
        System.out.println(jobs);
        return map;
    }

    @RequestMapping("/sendJob")
    @ResponseBody
     public int sendJob(HttpSession session , @RequestParam("resume_id") int resume_id, @RequestParam("job_id") int job_id) throws Exception{

        try{
            if (resume_id <= 0 && job_id <= 0) {
                return 0;
            }
            if (homeResumeService.checkIsSend(resume_id, job_id)==0) {
                int company_id = (Integer) session.getAttribute("company_id");
                Invite invite = new Invite();
                invite.setCompany_id(company_id);
                invite.setJob_id(job_id);
                invite.setResume_id(resume_id);
                invite.setCustomer_id(homeResumeService.selectCustomerIdByResumeId(resume_id));
                homeResumeService.insertInvite(invite);
                return 1;
            }else {
                return -1;
            }
        }catch (Exception e){
            return 0;
        }
    }

    @RequestMapping("resumeManage")
    public String resumeManage(Model model, @RequestParam("job_id") int job_id) throws Exception
    {
        model.addAttribute("job_id",job_id);
        return "home/resume/resumeManage";
    }

    @RequestMapping("/getResumeManageList")
    @ResponseBody
    public Map getResumeManageList(@RequestParam("p") int p,@RequestParam("job_id") int job_id,@RequestParam("select") int select_num) throws Exception
    {
        ManagePage managePage = new ManagePage();
        managePage.setJob_id(job_id);
        managePage.setCurrentPage(p);
        managePage.setPageSize(10);
        List<Resume> resumeList = new ArrayList<Resume>();
        if (select_num != 3) {
            if (select_num == 1){
                managePage.setDelivery_status(0);
            }else if (select_num == 2) {
                managePage.setDelivery_status(1);
            }else if (select_num == 4) {
                managePage.setDelivery_status(2);
            }
            managePage.setTotalRows(homeResumeService.countResumeManageRows(managePage));
            resumeList = homeResumeService.selectResumeManageByJobId(managePage);
        }else {
            managePage.setTotalRows(homeResumeService.countResumeManageRows2(managePage));
            resumeList = homeResumeService.selectResumeManageByJobId2(managePage);
        }

        Map map = new HashMap();
        map.put("data",resumeList);
        map.put("show", managePage);
        return map;
    }

    @RequestMapping("rManageDetail")
    public String rManageDetail(Model model, @RequestParam("resume_id") Integer resume_id, @RequestParam("select") Integer select, @RequestParam("delivery_id") Integer delivery_id) throws Exception
    {
        Resume resume = homeResumeService.selectResumeById(resume_id);
        System.out.println(resume.getExpect_city().getZone_id());
        System.out.print(resume.getExpect_city().getZone().getName());
        model.addAttribute("resume", resume);
        List<WorkExp> workExpList = homeResumeService.selectWorkByResumeId(resume.getResume_id());
        model.addAttribute("workExpList",workExpList);
        List<Education> educationList = homeResumeService.selectEduByResumeId(resume.getResume_id());
        model.addAttribute("educationList",educationList);
        List<CompetitionExp> competitionExpList = homeResumeService.selectComByResumeId(resume.getResume_id());
        model.addAttribute("competitionExpList",competitionExpList);
        List<Skill> skillList = homeResumeService.selectSkillByResumeId(resume.getResume_id());
        model.addAttribute("skillList",skillList);
        model.addAttribute("select",select);
        model.addAttribute("delivery_id",delivery_id);
        return "home/resume/rManageDetail";
    }

    @RequestMapping("/updateStatus")
    @ResponseBody
    public int updateStatus(@RequestParam("delivery_id") int id,@RequestParam("status") int status ) throws Exception
    {
        try {
            if (id <= 0) {
                return -1;
            }
            homeResumeService.updateDeliveryStatus(id,status);
            return 1;
        }catch (Exception e){

            return 0;
        }
    }
}
